package net.wing.demo.service;

import com.alibaba.fastjson.JSONObject;
import net.wing.demo.dao.MessageMapper;
import net.wing.demo.enums.ResEnum;
import net.wing.demo.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @Author beifengtz
 * @Date Created in 8:58 2018/8/10
 * @Description:
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    /**
     * @Author beifengtz
     * @Date Created in 2018/8/10 9:06
     * @Description: 获取所有留言信息的业务逻辑层
     * @param none
     * @return jsonobject
     */
    public JSONObject getAllMessages(){
        JSONObject res = new JSONObject();
        ArrayList messageList = messageMapper.selectAllMessage();
        if(messageList != null){
            res.put("code",ResEnum.SUCCESS.getCode());
            res.put("msg",ResEnum.SUCCESS.getMsg());
            res.put("data",messageList);
        }else{
            res.put("code",ResEnum.SYSTEM_ERROR.getCode());
            res.put("msg",ResEnum.SYSTEM_ERROR.getMsg());
            res.put("data",null);
        }
        return res;
    }

    public JSONObject setMessage(String content, int date) {
        JSONObject res = new JSONObject();
        if(content.equals("") || date/1000000000 < 1){
            res.put("code",ResEnum.PARAM_ILLEGAL.getCode());
            res.put("msg",ResEnum.PARAM_ILLEGAL.getMsg());
            res.put("data",null);
        }else{
            if(StrUtil.filterSqlStr(content)){
                if(messageMapper.insertMessage(content,date) > 0){
                    res.put("code",ResEnum.SUCCESS.getCode());
                    res.put("msg",ResEnum.SUCCESS.getMsg());
                    res.put("data",null);
                }else{
                    res.put("code",ResEnum.SYSTEM_ERROR.getCode());
                    res.put("msg",ResEnum.SYSTEM_ERROR.getMsg());
                    res.put("data",null);
                }
            }else{
                res.put("code",ResEnum.PARAM_ILLEGAL.getCode());
                res.put("msg",ResEnum.PARAM_ILLEGAL.getMsg());
                res.put("data",null);
            }
        }
        return res;
    }
}
